<?php


namespace app\index\controller;

use app\index\model\Action;
use app\index\model\ActionRole;
use app\index\model\Group;
use app\index\model\MenuRole;
use app\index\model\Role;
use think\Db;
use think\Request;
use think\response\Json;

class RoleController extends BaseController
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * updateRole
     * 更新权限菜单
     * @url /role/update
     * @method POST
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @author Asira
     * @date 2021/1/26
     */
    public function updateRole(): Json
    {
        $id=input('post.id/d',0);
        $type=input('post.type');
        $menu=input('post.menu/a');
        sort($menu);
        if(!$id){
            return json(['code'=>1,'msg'=>'ID不能为空']);
        }
        if($type==='role'){
            ['status'=>$status,'result'=>$result]=MenuRole::updateMenuRole($id,$menu);
        }else if($type==='role_detail'){
            ['status'=>$status,'result'=>$result]=MenuRole::updateMenuRoleDetail($id,$menu);
        }else if($type==='group'){
            ['status'=>$status,'result'=>$result]=MenuRole::updateMenuGroup($id,$menu);
        }else if($type==='user'){
            ['status'=>$status,'result'=>$result]=MenuRole::updateMenuUser($id,$menu);
        }else{
            return json(['code'=>1,'msg'=>'Type不能为空']);
        }
        if($status===false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }

    public function createRole(){
        $data=input('post.');
        if(isset($data['type']) && $data['type']==='role'){
            $role=$data['role'];
            if(!$role){
                return json(['code'=>1,'msg'=>'权限不能为空']);
            }
            $role_detail=$data['role_detail'];
            $menu=$data['menu'];
            ['status'=>$status,'result'=>$result]=MenuRole::createMenuRole($role,$role_detail,$menu);
        }else if ($data['type']==='group'){
            $group=$data['group'];
            if(!$group){
                return json(['code'=>1,'msg'=>'组名不能为空']);
            }
            $menu=$data['menu'];
            ['status'=>$status,'result'=>$result]=MenuRole::createMenuRoleGroup($group,$menu);
        }else{
            return json(['code'=>1,'msg'=>'type不能为空']);
        }
        if($status===false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }
    public function getRoleParam()
    {
        $action_list=Action::getActionList();
        $role_list=Role::getRoleCascadeList();
        $group_list=Group::getGroupList();
        return json(['code'=>0,'role_list'=>$role_list,'group_list'=>$group_list,'action_list'=>$action_list]);
    }
    public function getUserAction()
    {

        $id=input('post.id');
        $action_list=Action::getActionList();
        $type=input('post.type');
        if ($type=='user'){
            $where=['user_id'=>$id];
        }elseif ($type=='role'){
            $where=['role_id'=>$id];
        }elseif ($type=='role_detail'){
            $where=['role_detail_id'=>$id];
        }elseif ($type=='group'){
            $where=['group_id'=>$id];
        }
        $arr=Db::name('action_role')->where($where)->column('action_type,del_flag','action_id');
        foreach($action_list as $k=>$v){
            if (isset($arr[$v['id']])&&$arr[$v['id']]['del_flag']==0){
                $action_list[$k]['check']=true;
                $action_list[$k]['action_type']=$arr[$v['id']]['action_type'];
            }else{
                $action_list[$k]['check']=false;
                $action_list[$k]['action_type']=0;
            }
        }
        return json(['code'=>0,'action_list'=>$action_list]);
    }
    public function submitUser()
    {
        $id=input('post.id');
        $user=input('post.');
        $list=$user['user'];
        $type=$user['type'];
        if ($type=='user'){
            $where['user_id']=$id;
        }else if($type=='role'){
            $where['role_id']=$id;
        }else if($type=='role_detail'){
            $where['role_detail_id']=$id;
        }else if($type=='group'){
            $where['group_id']=$id;
        }
        $row=Db::name('action_role')->where($where)->column('id,action_type,del_flag','action_id');
//        dump($list);dump($row);die();
        ['status'=>$status,'result'=>$result]=ActionRole::updateActionRole($list,$row,$type,$id);
        if($status===false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>'添加成功','type'=>$type]);
    }
}